
import { _decorator, Component, Node, v3, Tween, tween, Sprite, v2 } from 'cc';
import PlayerData from '../../Script/Common/PlayerData';
import yyComponent from '../../Script/Common/yyComponent';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import { AwardPage } from './awardPage';
import { SpinAwardCfg } from './SpinAwardCfg';
const { ccclass, property } = _decorator;

@ccclass('SpinCmp')
export class SpinCmp extends yyComponent {
    @property(Node)
    mask: Node = null;
    @property(AwardPage)
    awardPage: AwardPage = null;

    //按照顺序设定概率-分别对应 0~7 的概率
    _cfg = [];

    _ang = v3();
    onLoad() {
        //初始化概率
        let i = 0;
        for (const key in SpinAwardCfg) {
            this._cfg[i] = SpinAwardCfg[key].rate;
            i++;
        }
    }

    onEnable() {
        this.resetSpin();
    }

    //重置
    resetSpin() {
        this.mask.active = false;
        this.awardPage.hide();

        Tween.stopAllByTarget(this._ang);
        this._ang.set(0, 0, 0);
        this.node.eulerAngles = this._ang;
    }

    //执行动画/指定转到某个物品
    runSpinAnim(itemId?: number) {
        this.emit(EventType.Common.AudioEvent.playEffect, GlobalEnum.AudioClip.spin);

        this.mask.active = true;

        if (itemId == undefined) {
            itemId = this.getRandItemId();
        }
        // this.resetSpin();
        Tween.stopAllByTarget(this._ang);
        //
        let lastAng = this.node.eulerAngles.z;
        lastAng = (lastAng + 360) % 360;
        this._ang.set(0, 0, lastAng);
        //计算目标角度    
        let toAng = -(itemId * 45 + 22.5);
        //放大旋转次数
        toAng += 360 * 8;

        tween(this._ang).to(3, { z: toAng }, {
            easing: 'sineInOut', onUpdate: () => {
                this.node.eulerAngles = this._ang;
            }
        }).call(() => {
            this.runFinished(itemId);
        }).start();
    }
    //执行完毕回调
    runFinished(itemId: number) {

        this.mask.active = false;
        //展示奖励
        let _cfg = SpinAwardCfg[itemId];
        if (_cfg.spinType == 0) { //金币类型奖励
            //播放金币动画
            this.emit(EventType.Common.UIEvent.playGoldAnim, {
                startPos: v2(this.node.position.x, this.node.position.y),
                gold: _cfg.val,
                cb: () => {
                    this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                        attribute: "gameData.asset.gold",
                        value: _cfg.val,
                        mode: "+",
                        emit: true,
                        save: true,
                    });
                }
            });

        } else { //商品奖励
            let sf = this.node.children[itemId].getComponent(Sprite).spriteFrame;
            this.awardPage.showAward(sf, _cfg.shopType, _cfg.shopId);
        }
    }

    //根据概率随机一个物品Id
    getRandItemId() {
        let arr: { id: number, rate: number }[] = [];
        for (let i = 0; i < this._cfg.length; i++) {
            arr.push({ id: i, rate: this._cfg[i] });
        }
        //从小到大排序
        arr.sort((a, b) => a.rate - b.rate);

        let rate = 0;
        let randRate = Math.random();
        let itemId = 0;
        for (let n = 0; n < arr.length; n++) {
            const e = arr[n];
            rate += e.rate;
            if (randRate <= rate) {
                itemId = e.id;
                break;
            }
        }
        console.log('====转盘奖励:' + itemId + ' ' + SpinAwardCfg[itemId].val);
        return itemId;
    }


    //------------EVents---------------
    onRunClick() {
        let data = PlayerData.getPlayerData();
        let isFree = data.spinData.count <= 3;
        if (isFree) {
            this.runSpinAnim();
        } else {
            this.emit(EventType.Common.SDKEvent.showVideo, () => {
                this.runSpinAnim();
            })
        }

    }

    onRunClickByVideo() {
        this.emit(EventType.Common.SDKEvent.showVideo, () => {
            this.runSpinAnim();
        });
    }
}
